//-----------------------------------------------------------------------
//<copyright file="AccountDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class AccountDAO : SQLDatabase, IAccountDAO
	{

		#region Create Account methods

		public Account CreateAccount(Account account)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spAccountInsert");
				AddInParameter(command, "@ID", SqlDbType.NVarChar, account.ID);
				AddInParameter(command, "@Matkhau", SqlDbType.NVarChar, account.Matkhau);
				AddInParameter(command, "@Hoten", SqlDbType.NVarChar, account.Hoten);
				AddInParameter(command, "@Diachi", SqlDbType.NVarChar, account.Diachi);
				AddInParameter(command, "@SoDT", SqlDbType.NVarChar, account.SoDT);
				AddInParameter(command, "@Email", SqlDbType.NVarChar, account.Email);
				AddInParameter(command, "@Chucvu", SqlDbType.NVarChar, account.Chucvu);
				AddInParameter(command, "@Thongtinkhac", SqlDbType.NVarChar, account.Thongtinkhac);
				AddInParameter(command, "@Role", SqlDbType.Int, account.Role);

				ExecuteNonQuery(command);
				return account;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreateAccountException", ex);
			}
		}

		#endregion

		#region Update Account methods

		public void UpdateAccount(Account account)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spAccountUpdate");

				AddInParameter(command, "@ID", SqlDbType.NVarChar, account.ID);
				AddInParameter(command, "@Hoten", SqlDbType.NVarChar, account.Hoten);
				AddInParameter(command, "@Diachi", SqlDbType.NVarChar, account.Diachi);
				AddInParameter(command, "@SoDT", SqlDbType.NVarChar, account.SoDT);
				AddInParameter(command, "@Email", SqlDbType.NVarChar, account.Email);
				AddInParameter(command, "@Chucvu", SqlDbType.NVarChar, account.Chucvu);
				AddInParameter(command, "@Thongtinkhac", SqlDbType.NVarChar, account.Thongtinkhac);
				AddInParameter(command, "@Role", SqlDbType.Int, account.Role);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdateAccountException", ex);
			}
		}

        public void ChangePasswprd(String ID, String NewPass)
        {
            try
            {
                SqlCommand command = SQLDatabase.StoredProcedureCommand("spAccountChangePassword");

                AddInParameter(command, "@ID", SqlDbType.NVarChar, ID);
                AddInParameter(command, "@Matkau", SqlDbType.NVarChar, NewPass);
                

                ExecuteNonQuery(command);
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access UpdateAccountException", ex);
            }
        }

		#endregion

		#region Delete Account methods

		public void DeleteAccount(Account account)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spAccountDelete");

				AddInParameter(command, "@Id", SqlDbType.NVarChar, account.ID);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeleteAccountException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll Account methods

		public Accounts GetAllAccount()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spAccountGetList");
				Accounts Result = new Accounts();
				List<Account> List = ExecuteQuery<Account>(command,true);
				foreach(Account item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllAccountException", ex);
			}
		}

		#endregion


		#region GetAccount methods

		public Account GetAccountByID(string id)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spAccountGet");

				AddInParameter(command, "@id", SqlDbType.NVarChar, id);

				return ExecuteQuery<Account>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAccountBy ID Exception", ex);
			}
		}

        public Account loogin(string Usernanme, string pass)
        {
            try
            {
                SqlCommand command = SQLDatabase.StoredProcedureCommand("spAccountLogin");

                AddInParameter(command, "@ID", SqlDbType.NVarChar, Usernanme);
                AddInParameter(command, "@MatKhau", SqlDbType.NVarChar, pass);

                return ExecuteQuery<Account>(command);
            }
            catch (Exception ex)
            {
                throw DataAccessException.GetException("Data Access GetAccountBy ID Exception", ex);
            }
        }

		#endregion


		#endregion
	}
}//End Class
